package com.usian.controller;

import com.github.pagehelper.PageInfo;
import com.usian.feedback.Feedback;
import com.usian.game.GameLogs;
import com.usian.message.Message;
import com.usian.role.Role;
import com.usian.room.Room;
import com.usian.service.SystemService;
import com.usian.user.User;
import com.usian.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 米西米西
 */
@RequestMapping("/api/system")
@RestController
public class SystemController {

    @Autowired
    SystemService systemService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @author 米西米西
     * 接口：分页查询房间信息
     */
    @PostMapping("/findRoom/{pageNum}/{pageSize}")
    public Result findRoom(@PathVariable Integer pageNum,@PathVariable Integer pageSize, @RequestBody Room room){
        PageInfo<Room> pageInfo = systemService.findRoom(pageNum, pageSize, room);
        return Result.success(pageInfo);
    }

    /**
     * @author 米西米西
     * 接口：查询所有用户信息
     */
    @GetMapping("/userFindAll")
    public Result userFindAll(User user){
        List<User> userList = systemService.userFindAll(user);
        return Result.success(userList);
    }
    /**
     * @author 米西米西
     * 接口：根据房间id查询房间里面的用户信息
     */
    @GetMapping("/userFindUserId/{id}")
    public Result userFindUserId(@PathVariable Integer id){
        List<User> userList = systemService.userFindUserId(id);
        return Result.success(userList);
    }
    /**
     * @author 米西米西
     * 接口：根据房间id查询房间里面的聊天记录
     */
    @GetMapping("/messageFindUserId/{id}")
    public Result messageFindUserId(@PathVariable Integer id){
        List<Message> message = redisTemplate.opsForList().range(id+"message", 0, -1);
        return Result.success(message);
    }

    /**
     * @author 米西米西
     * 接口：分页查询用户信息
     */
    @PostMapping("/findUserPage/{pageNum}/{pageSize}")
    public Result findUserPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize, @RequestBody User user){
        PageInfo<User> pageInfo = systemService.findUserPage(pageNum, pageSize, user);
        return Result.success(pageInfo);
    }

    /**
     * @author 米西米西
     * 接口：分页查询角色信息
     */
    @PostMapping("/findRolePage/{pageNum}/{pageSize}")
    public Result findRolePage(@PathVariable Integer pageNum,@PathVariable Integer pageSize, @RequestBody Role role){
        PageInfo<Role> pageInfo = systemService.findRolePage(pageNum, pageSize, role);
        return Result.success(pageInfo);
    }

    /**
     * @author 米西米西
     * 接口：房间内踢出玩家
     */
    @PostMapping("/delRoomUser/{roomid}/{userId}")
    public Result delRoomUser(@PathVariable Integer roomid,@PathVariable Integer userId){
        systemService.delRoomUser(roomid,userId);
        return Result.success();
    }
    /**
     * @author 米西米西
     * 接口：获取房主id
     */
    @GetMapping("/getRoomUser/{roomid}")
    public Result getRoomUser(@PathVariable Integer roomid){
        Integer roomUser = systemService.getRoomUser(roomid);
        return Result.success(roomUser);
    }
    /**
     * @author 米西米西
     * 接口：获取房间开始之后的玩家操作信息
     */
    @GetMapping("/getGameUserLogs/{roomid}")
    public Result getGameUserLogs(@PathVariable Integer roomid){
        List<GameLogs> gameLogs = systemService.getGameUserLogs(roomid);
        return Result.success(gameLogs);
    }
    /**
     * @author 米西米西
     * 接口：修改用户信息
     */
    @PostMapping("/user/update")
    public Result updateUser(@RequestBody User user){
        Integer i = systemService.updateUser(user);
        if (i == null){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    /**
     * @author 米西米西
     * 接口：分页展示意见信息
     */
    @PostMapping("/getFeedback/{pageNum}/{pageSize}")
    public Result getOpinion(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody Feedback feedback){
        PageInfo<Feedback> pageInfo = systemService.getFeedback(pageNum, pageSize, feedback);
        return Result.success(pageInfo);
    }













}
